Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Capturing file modified date | filelist

    I wanted to list most recently updated file within a folder and needed to capture all file modified dates. After trying ls fs etc, I came upon filelist (ssc program created by Robert Picard) which return all files and 4 macros which I am not sure how to access. Do they involve dates? If anyone has a better way to access file modified dates so I can code to list files updated in the past 7 days, it will be useful.

    Code:
    /* 
    local roots ///
        "C:/Users/Vijayalakshmi/Dropbox/Girish Files" ///
        "C:/Users/NewPC/Dropbox/Girish Files" ///
        "D:/gvenkataraman/Documents/Dropbox/Girish Files" ///
        "D:/Dropbox/Girish Files"
    
    local found 0
    foreach r of local roots {
        capture cd "`r'"
        if !_rc {
            local found 1
            local dropbox "`r'"
            continue, break
        }
    }
    if !`found' {
        di as error "Dropbox path not found on this PC."
        exit 198
    }
    
    local stem "`dropbox'/gv_programs"
    di "------------------------------------------------------------"
    di "Dropbox base :  `dropbox'"
    di "GV programs  :  `stem'"
    di "------------------------------------------------------------"
    
    
    filelist, dir("`stem'") pattern("*.ado") list
    Output from that is
    Code:
    Number of files found = 45
    
      +---------------------------------------------------------------------------+
      | filepath                                                            fsize |
      |---------------------------------------------------------------------------|
      | C:/Users/NewPC/Dropbox/Girish Files/gv_programs/cdgv.ado              261 |
      | C:/Users/NewPC/Dropbox/Girish Files/gv_programs/coxer.ado           3,530 |
    ....
    ....
    
    end of do-file
    
    . ret li
    
    scalars:
                r(changed) =  1
                  r(width) =  129
                      r(k) =  4
                      r(N) =  45

  • #2
    ...filelist (ssc program created by Robert Picard) which return all files and 4 macros which I am not sure how to access. Do they involve dates?
    No, those macros have nothing to do with dates. Rather they are exactly what you would see if you simply ran -describe- on the data set created by filelist. r(changed) = 1 indicates that the data set has changed since its last -save-. -r(width)- is the number of bytes of storage for each observation. -r(N)- is the number of observations, and -r(k)- is the number of variables in the data set.

    You can access these numbers, should you find them useful, the same way you can access contents of -r()- after any program is run. For example, to store the value of -r(width)- in a local macro you could run
    Code:
    local record_bytes = r(width)
    
    // OR
    
    local record_bytes `r(width)'
    Remember that the contents of -r()- are volatile: they will be overwritten as soon as any subsequent command itself leaves behind information in -r()-. So commands like the above to store the value of something in -r()- should be run immediately after the command that produced them.

    All of that said, it is unlikely that any of these -r()- macros will be useful to you for your current purpose. I do not think that -filelist- can be made to provide file modification dates. Nor do I know of any Stata commands that would retrieve this information for you. I think you would have to write a program of your own to do that. The way to accomplish it that comes to my mind involves several steps:
    1. Fork a command to your operating system to list the files in question and redirect its output to a text file.
    2. Use the -file- commands to open that text file and read it in one line at a time.
    3. Loop over the lines in the text file, parsing each line to extract the filename and the modification date: regular expressions will probably be the best way to do that.
    4. Store those results somewhere useful: perhaps in a -postilfe- or -frame-.
    5. Write a few lines of code to create a variable indicating those files whose date is within 7 days of the current date.
    Hope this helps. Maybe somebody else knows a better way, too.

    Added: Actually, I just thought of a better way. In Windows, and probably in other operating systems you are likely to encounter, the text file created by step 1 above is a fixed-width text file. So instead of steps 2 through 4, you can use -infix- to import it. (First you'll have to invest a little time counting out the width of each field in the output.) Then you can skip to step 5, which is just a matter of converting the date-as-string variable to a Stata internal format date variable with the -daily()- function and comparing that to the current date.
    Last edited by Clyde Schechter; 07 Oct 2025, 22:07.

    Comment


    • #3
      Thanks Clyde Schechter. Glad to know it is more involved and elusive. I will use your steps above as a starting point and see if I can craft a method (and share if it works well). My objective was only to move recently updated personally created programs in my Dropbbox to the Stata PERSONAL directory of the PC I am working off.

      Comment


      • #4
        Is your goal to have these as data for subsequent use in or manipulation by Stata? Or do you just want to be able to see the information?

        If the latter then there are a few ways to do this with unix commands, which you can use on your PC via Windows Subsystem for Linux.

        Example using find:
        https://stackoverflow.com/a/16086041

        Example using ls:
        https://stackoverflow.com/a/32151987

        You could then send ("pipe") the results to a file that Stata could read.

        There may be python tools that do similar things, the advantage being that you could use them within Stata .



        Comment


        • #5
          There is an old community-contributed command that pretty much does what Clyde Schechter recommended. Try ssc install dirlist . The program was last modified in 2005 though, so your mileage may vary.

          Comment


          • #6
            Originally posted by Bert Lloyd View Post
            Is your goal to have these as data for subsequent use in or manipulation by Stata? Or do you just want to be able to see the information?

            If the latter then there are a few ways to do this with unix commands, which you can use on your PC via Windows Subsystem for Linux.

            Example using find:
            https://stackoverflow.com/a/16086041

            Example using ls:
            https://stackoverflow.com/a/32151987

            You could then send ("pipe") the results to a file that Stata could read.

            There may be python tools that do similar things, the advantage being that you could use them within Stata .


            True. This was honestly just a way to move most recently updated ado files to the PERSONAL dir across all PCs and keep it synced so any downstream analysis script using the PERSONAL ado programs use updated versions all the time. Python actually made short work of it since there are specific libraries that capture this information easily. I was trying to practice to get a Stata only method so I could call this helper script to keep PERSONAL synced before downstream use of the programs.

            Comment


            • #7
              Originally posted by Hemanshu Kumar View Post
              There is an old community-contributed command that pretty much does what Clyde Schechter recommended. Try ssc install dirlist . The program was last modified in 2005 though, so your mileage may vary.
              Got the perfect milieage, Hemanshu Kumar . dirlist by Morten Andersen solved the issue. Was able to capture the needed info which I can now wrap to tag most recently updated file to be moved into the PERSONAL directory. For those who want to see how I tagged it. See below

              Code:
              dirlist kms.ado
              
              ret li
              
              macros:
                           r(nfiles) : "1"
                           r(fsizes) : "7,456"
                           r(ftimes) : "08:43-PM"
                           r(fdates) : "10/06/2025"
                           r(fnames) : "kms.ado
              
              local fdate  "`r(fdates)'"
              local filedate = date("`fdate'", "MDY")
              local today = date("`c(current_date)'", "DMY")
              local diff = abs(`today' - `filedate')
              local within7 = (`diff' <= 7)
              
              display "File date: `fdate'"
              display "Days difference: " `diff'
              display "Within 7 days: " `within7'
              
              
              
              . display "File date: `fdate'"
              File date: 10/06/2025
              
              . display "Days difference: " `diff'
              Days difference: 3
              
              . display "Within 7 days: " `within7'
              Within 7 days: 1
              Last edited by Girish Venkataraman; 09 Oct 2025, 06:57. Reason: dirlist not dirtools

              Comment

              Working...
              X